package tree_util;

import com.common.PrimTool;
import com.common.SampleInfoTool;
import com.opensymphony.xwork2.ActionSupport;
import db_tool.LogTool;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 10742 on 2017/7/9.
 */
public class Op_SampleInfo extends ActionSupport {

    private int id;
    private int moleculeID;
    private String flag;
    private String gene;
    private String sequence;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getMoleculeID() {
        return moleculeID;
    }

    public void setMoleculeID(int moleculeID) {
        this.moleculeID = moleculeID;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getGene() {
        return gene;
    }

    public void setGene(String gene) {
        this.gene = gene;
    }

    public String getSequence() {
        return sequence;
    }

    public void setSequence(String sequence) {
        this.sequence = sequence;
    }

    @Override
    public String execute() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        SampleInfoTool tool = (SampleInfoTool) context.getBean("sampleTool");
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        System.out.println("this is my flag " + flag);
        if (flag.equals("insert")) {
            id = tool.insert(sequence, gene, moleculeID);
            LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "Sample_Info", 1);
            writer.write(String.valueOf(id));
            writer.close();
        } else if (flag.equals("update")) {
            int column = tool.update(id, sequence, gene);
            LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "Sample_Info", 1);
            if (column == 1){
                writer.write("true");
            }
            writer.close();
        }
        return null;
    }

}
